"""
抓取GitHub主页的样式风格，用于AI参考复刻网站
提取主要的颜色、字体、间距、布局等设计元素
"""

import requests
import re
import json
from bs4 import BeautifulSoup
from collections import Counter
from urllib.parse import urljoin, urlparse

HEADERS = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
}


def fetch_page(url):
    """获取网页内容"""
    try:
        resp = requests.get(url, headers=HEADERS, timeout=15)
        resp.encoding = 'utf-8'
        return resp.text, resp.url
    except Exception as e:
        print(f"获取页面失败: {e}")
        return None, None


def extract_colors(css_text):
    """从CSS中提取颜色值"""
    colors = {
        'hex': [],
        'rgb': [],
        'rgba': [],
        'named': []
    }
    
    # 提取hex颜色
    hex_pattern = r'#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})\b'
    hex_colors = re.findall(hex_pattern, css_text)
    colors['hex'] = ['#' + c if len(c) == 6 else '#' + c[0]*2 + c[1]*2 + c[2]*2 for c in hex_colors]
    
    # 提取rgb颜色
    rgb_pattern = r'rgb\(([^)]+)\)'
    colors['rgb'] = re.findall(rgb_pattern, css_text)
    
    # 提取rgba颜色
    rgba_pattern = r'rgba\(([^)]+)\)'
    colors['rgba'] = re.findall(rgba_pattern, css_text)
    
    # 提取命名颜色
    named_colors = ['black', 'white', 'red', 'blue', 'green', 'yellow', 'orange', 
                   'purple', 'pink', 'gray', 'grey', 'transparent']
    for color in named_colors:
        if re.search(rf'\b{color}\b', css_text, re.I):
            colors['named'].append(color)
    
    return colors


def extract_fonts(css_text):
    """提取字体相关样式"""
    fonts = {
        'font_families': [],
        'font_sizes': [],
        'font_weights': [],
        'line_heights': []
    }
    
    # 提取font-family
    font_family_pattern = r"font-family:\s*([^;]+)"
    font_families = re.findall(font_family_pattern, css_text, re.I)
    for ff in font_families:
        # 清理引号和空格
        cleaned = re.sub(r'["\']', '', ff).strip()
        fonts['font_families'].extend([f.strip() for f in cleaned.split(',')])
    
    # 提取font-size
    font_size_pattern = r"font-size:\s*([^;]+)"
    font_sizes = re.findall(font_size_pattern, css_text, re.I)
    fonts['font_sizes'] = [s.strip() for s in font_sizes]
    
    # 提取font-weight
    font_weight_pattern = r"font-weight:\s*([^;]+)"
    font_weights = re.findall(font_weight_pattern, css_text, re.I)
    fonts['font_weights'] = [w.strip() for w in font_weights]
    
    # 提取line-height
    line_height_pattern = r"line-height:\s*([^;]+)"
    line_heights = re.findall(line_height_pattern, css_text, re.I)
    fonts['line_heights'] = [h.strip() for h in line_heights]
    
    return fonts


def extract_spacing(css_text):
    """提取间距相关样式"""
    spacing = {
        'padding': [],
        'margin': [],
        'gap': []
    }
    
    # 提取padding
    padding_pattern = r"padding[^:]*:\s*([^;]+)"
    paddings = re.findall(padding_pattern, css_text, re.I)
    spacing['padding'] = [p.strip() for p in paddings]
    
    # 提取margin
    margin_pattern = r"margin[^:]*:\s*([^;]+)"
    margins = re.findall(margin_pattern, css_text, re.I)
    spacing['margin'] = [m.strip() for m in margins]
    
    # 提取gap
    gap_pattern = r"gap:\s*([^;]+)"
    gaps = re.findall(gap_pattern, css_text, re.I)
    spacing['gap'] = [g.strip() for g in gaps]
    
    return spacing


def extract_border_radius(css_text):
    """提取圆角样式"""
    border_radius_pattern = r"border-radius[^:]*:\s*([^;]+)"
    border_radiuses = re.findall(border_radius_pattern, css_text, re.I)
    return [br.strip() for br in border_radiuses]


def fetch_css_file(css_url, base_url):
    """获取外部CSS文件内容"""
    try:
        if css_url.startswith('//'):
            css_url = 'https:' + css_url
        elif css_url.startswith('/'):
            css_url = urljoin(base_url, css_url)
        elif not css_url.startswith('http'):
            css_url = urljoin(base_url, css_url)
        
        resp = requests.get(css_url, headers=HEADERS, timeout=10)
        return resp.text
    except Exception as e:
        print(f"获取CSS文件失败 {css_url}: {e}")
        return ""


def analyze_github_styles():
    """分析GitHub主页的样式风格"""
    url = "https://github.com/"
    
    print(f"正在抓取 {url} ...")
    html, final_url = fetch_page(url)
    
    if not html:
        return None
    
    base_url = f"{urlparse(final_url).scheme}://{urlparse(final_url).netloc}"
    soup = BeautifulSoup(html, 'html.parser')
    
    print("正在分析样式...")
    
    # 收集所有CSS内容
    all_css = ""
    
    # 1. 提取内联样式
    inline_styles = []
    for tag in soup.find_all(style=True):
        inline_styles.append(tag.get('style', ''))
        all_css += tag.get('style', '') + "\n"
    
    # 2. 提取<style>标签中的CSS
    style_tags = soup.find_all('style')
    for style_tag in style_tags:
        if style_tag.string:
            all_css += style_tag.string + "\n"
    
    # 3. 提取外部CSS文件
    css_files = []
    for link in soup.find_all('link', rel='stylesheet'):
        href = link.get('href', '')
        if href:
            css_files.append(href)
            css_content = fetch_css_file(href, base_url)
            all_css += css_content + "\n"
    
    print(f"找到 {len(css_files)} 个外部CSS文件")
    print(f"找到 {len(inline_styles)} 个内联样式")
    
    # 提取样式信息
    colors = extract_colors(all_css)
    fonts = extract_fonts(all_css)
    spacing = extract_spacing(all_css)
    border_radius = extract_border_radius(all_css)
    
    # 统计最常用的颜色（去重）
    all_hex_colors = list(set(colors['hex']))
    color_counter = Counter(all_hex_colors)
    top_colors = color_counter.most_common(20)
    
    # 统计最常用的字体
    font_family_counter = Counter(fonts['font_families'])
    top_fonts = font_family_counter.most_common(10)
    
    # 统计字体大小
    font_size_counter = Counter(fonts['font_sizes'])
    top_font_sizes = font_size_counter.most_common(15)
    
    # 提取关键布局信息
    layout_info = {
        'display_types': [],
        'flex_directions': [],
        'grid_templates': []
    }
    
    # 提取display
    display_pattern = r"display:\s*([^;]+)"
    displays = re.findall(display_pattern, all_css, re.I)
    layout_info['display_types'] = list(set([d.strip() for d in displays]))
    
    # 提取flex-direction
    flex_dir_pattern = r"flex-direction:\s*([^;]+)"
    flex_dirs = re.findall(flex_dir_pattern, all_css, re.I)
    layout_info['flex_directions'] = list(set([f.strip() for f in flex_dirs]))
    
    # 提取grid-template
    grid_pattern = r"grid-template[^:]*:\s*([^;]+)"
    grids = re.findall(grid_pattern, all_css, re.I)
    layout_info['grid_templates'] = [g.strip() for g in grids[:10]]  # 限制数量
    
    # 构建样式报告
    style_report = {
        'url': url,
        'colors': {
            'top_hex_colors': [{'color': c[0], 'frequency': c[1]} for c in top_colors],
            'rgb_colors': list(set(colors['rgb']))[:20],
            'rgba_colors': list(set(colors['rgba']))[:20],
            'named_colors': list(set(colors['named']))
        },
        'typography': {
            'font_families': [{'font': f[0], 'frequency': f[1]} for f in top_fonts],
            'font_sizes': [{'size': s[0], 'frequency': s[1]} for s in top_font_sizes],
            'font_weights': list(set(fonts['font_weights'])),
            'line_heights': list(set(fonts['line_heights']))[:20]
        },
        'spacing': {
            'common_padding': list(set(spacing['padding']))[:20],
            'common_margin': list(set(spacing['margin']))[:20],
            'common_gap': list(set(spacing['gap']))[:20]
        },
        'border_radius': list(set(border_radius))[:20],
        'layout': layout_info,
        'css_files_count': len(css_files),
        'inline_styles_count': len(inline_styles)
    }
    
    return style_report


def save_report(report, filename='github_styles_report.json'):
    """保存样式报告"""
    with open(filename, 'w', encoding='utf-8') as f:
        json.dump(report, f, ensure_ascii=False, indent=2)
    print(f"\n样式报告已保存到: {filename}")


def print_summary(report):
    """打印样式摘要"""
    print("\n" + "="*60)
    print("GitHub主页样式风格摘要")
    print("="*60)
    
    print("\n【主要颜色】")
    for i, color_info in enumerate(report['colors']['top_hex_colors'][:10], 1):
        print(f"  {i}. {color_info['color']} (出现{color_info['frequency']}次)")
    
    print("\n【字体家族】")
    for i, font_info in enumerate(report['typography']['font_families'][:5], 1):
        print(f"  {i}. {font_info['font']} (出现{font_info['frequency']}次)")
    
    print("\n【常用字体大小】")
    for i, size_info in enumerate(report['typography']['font_sizes'][:8], 1):
        print(f"  {i}. {size_info['size']} (出现{size_info['frequency']}次)")
    
    print("\n【常用间距】")
    padding_preview = report['spacing']['common_padding'][:5]
    margin_preview = report['spacing']['common_margin'][:5]
    print(f"  Padding示例: {', '.join(padding_preview)}")
    print(f"  Margin示例: {', '.join(margin_preview)}")
    
    print("\n【布局方式】")
    print(f"  Display类型: {', '.join(report['layout']['display_types'][:5])}")
    
    print("\n【统计数据】")
    print(f"  外部CSS文件: {report['css_files_count']} 个")
    print(f"  内联样式: {report['inline_styles_count']} 个")
    print("="*60)


if __name__ == "__main__":
    print("开始抓取GitHub主页样式...")
    report = analyze_github_styles()
    
    if report:
        print_summary(report)
        save_report(report, 'github_styles_report.json')
        print("\n完成！")
    else:
        print("抓取失败，请检查网络连接或稍后重试。")
